package com.joplus.housekeeper.adapter;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ExpandableListAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hyphenate.chatuidemo.ui.ChatActivityNew;
import com.joplus.housekeeper.R;
import com.joplus.housekeeper.bean.ImSearchAccountBean;
import com.joplus.housekeeper.bean.ImSearchBean;
import com.joplus.housekeeper.bean.ImSearchMessageBean;
import com.joplus.housekeeper.bean.MessageAccountListBean;
import com.joplus.housekeeper.ui.activity.ImRecordListActivity;
import com.joplus.housekeeper.utils.StringUtils;
import com.joplus.housekeeper.utils.T;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by zisheng on 2018/7/4.
 */

public class ImSearchAdapter extends BaseExpandableListAdapter {


    private SimpleDateFormat simpleDateFormat;

    public ImSearchAdapter() {

        //    size = 2 + (data.getMessageAccountWithBLOBsList() == null ? 0 : data.getMessageAccountWithBLOBsList().size()) + (data.getMessageRecordWithBLOBsList() == null ? 0 : data.getMessageRecordWithBLOBsList().size());
        simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 hh:mm");
    }

    @Override
    public int getGroupCount() {
        return 2;
    }

    List<ImSearchMessageBean.DataBean.MessageRecordWithBLOBsListBean> messageRecordWithBLOBsList;
    List<MessageAccountListBean> messageAccountListBean;

    public void setAccountRecord(List<MessageAccountListBean> messageRecordWithBLOBsList) {
        this.messageAccountListBean = messageRecordWithBLOBsList;
    }

    public void setMessageRecord(List<ImSearchMessageBean.DataBean.MessageRecordWithBLOBsListBean> messageAccountListBean) {
        this.messageRecordWithBLOBsList = messageAccountListBean;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        if (groupPosition == 0) {
            return messageRecordWithBLOBsList != null ? messageRecordWithBLOBsList.size() : 0;
        } else {
            return messageAccountListBean != null ? messageAccountListBean.size() : 0;
        }

    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupPosition;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_tv_1, null);
        TextView tv = (TextView) view.findViewById(R.id.tv);
        if (groupPosition == 0) {
            tv.setText("用户记录");
        } else {
            tv.setText("聊天记录");
        }
        return view;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, final ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_convertisement_search, null);
        CircleImageView iv = (CircleImageView) view.findViewById(R.id.iv);
        TextView tv_name = (TextView) view.findViewById(R.id.tv_name);
        TextView tv_last = (TextView) view.findViewById(R.id.tv_last);
        TextView tv_time = (TextView) view.findViewById(R.id.tv_time);

        if (groupPosition == 0) {

            ImSearchMessageBean.DataBean.MessageRecordWithBLOBsListBean messageRecordWithBLOBsListBean = messageRecordWithBLOBsList.get(childPosition);

            // ImageLoader.getInstance().displayImage(messageRecordWithBLOBsListBean.getac(), iv);
            //StringUtils.setText(tv_name, messageRecordWithBLOBsListBean.get());
            Date date = new Date(messageRecordWithBLOBsListBean.getMessageDatetime());
            StringUtils.setText(tv_time, simpleDateFormat.format(date));
            if (messageRecordWithBLOBsListBean.getType() == 1) {
                StringUtils.setText(tv_last, messageRecordWithBLOBsListBean.getDecryptContent());
            } else {
                StringUtils.setText(tv_last, "理论上不可能搜索出来的内容..我承认是bug~.~");
            }
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                   /* Intent intent = new Intent(parent.getContext(), ImRecordListActivity.class);
                    intent.putExtra("messageDate", messageRecordWithBLOBsListBean.getMessageDatetime());
                    intent.putExtra("accountId", messageRecordWithBLOBsListBean.getAccountId());
                    parent.getContext().startActivity(intent);*/
                    T.showShort(parent.getContext(), "没有用户id,就不让你去体验闪退这个功能了");
                }
            });
        } else {

            final MessageAccountListBean messageAccountWithBLOBsListBean = this.messageAccountListBean.get(childPosition);
            ImageLoader.getInstance().displayImage(messageAccountWithBLOBsListBean.getAvatar(), iv);
            StringUtils.setText(tv_name, messageAccountWithBLOBsListBean.getName());
            Date date = new Date(messageAccountWithBLOBsListBean.getMessageDatetime());
            StringUtils.setText(tv_time, simpleDateFormat.format(date));
            StringUtils.setText(tv_last, messageAccountWithBLOBsListBean.getLastMessageContent());
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Intent intent = new Intent(parent.getContext(), ChatActivityNew.class);
                    intent.putExtra("userId", messageAccountWithBLOBsListBean.getImId());
                    intent.putExtra("user", messageAccountWithBLOBsListBean);
                    parent.getContext().startActivity(intent);

                }
            });

        }
        return view;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }
}
